package com.ma.chapter4.t7;

import com.ma.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName TestReentrantLock3
 * @Author: mayongqiang
 * @DATE 2022/3/14 23:36
 * @Description: 1、lock.tryLock() 立刻失败返回
 * 主动打断自己，不会死等，放弃等待
 */
@Slf4j(topic = "m.TestReentrantLock3")
public class TestReentrantLock3 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            log.debug("启动");
            if (!lock.tryLock()) {
                log.debug("获取立刻失败，返回");
                return;
            }
            try {
                log.debug("获得了锁");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        log.debug("获得了锁");
        t1.start();
        Sleeper.sleep(2);
        lock.unlock();
    }
}
